let $guding;
let $xialajiazai;
let $xialajiazaineirong;
let flush_data;
let $zhezhao;
let boo =false;
let speed = 500;
$(()=>{
	
	let $yzmpic = $(`#yzmpic`);
	let $yzm = $(`#yzm`);
	let $valuename = $("#valuename")
	let msgtype = 0;
	let msgtarget = "";
	let msgname = "";
	let $new = $("#new");
	let yz_yzm = false;
	let $send = $("#send")
	let $flush = $("#flush")
	let $textarea = $("#textarea")
	
	let formatdate = (date)=>{
		let nian,yue,ri,shi,fen,miao
		nian = date.getFullYear()
		yue = date.getMonth()+1
		ri = date.getDate()
		shi = date.getHours()
		fen = date.getMinutes()
		miao = date.getSeconds()
		return `${nian}年${yue}月${ri}日&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${shi}:${fen}:${miao}`
	}
	$new.click(()=>{
		$valuename.val("");
		$valuename.removeAttr("disabled")
		msgtype = 0
		msgtarget =""
		msgname =""
	})
	// 验证码验证
	$yzm.blur(()=>{
		let yzmval = $yzm.val();
		if($yzmpic.attr("val").toUpperCase()!=$.md5(yzmval.toLowerCase()).toUpperCase()){
				yz_yzm=false;
				$yzm.css("color","#ff0000")
		}else{
				yz_yzm=true;
				$yzm.css("color","#008800")
		}
	})
	$send.click(()=>{
		let msgvalue = ""
		if(msgtype ==0 && $valuename.val() != ""){
			msgname = $valuename.val()
			msgtype = 1
		}
		msgvalue = $textarea.val()
		if(yz_yzm==true&&msgvalue!=null&&msgvalue.trim().length!=0){
			$.ajax({
				url:"lybsenddata",
				data:{
					type:msgtype,
					target:msgtarget,
					name:msgname,
					value:msgvalue,
					yzm:$yzm.val()
				},
				dataType:"json",
				success:(data)=>{
					if(data.code != 200) alert(data.msg)
					$flush.click()
				}
			})
		}
	})
	$yzmpic.click(()=>{
		setTimeout(()=>{
			$.ajax({
				url:"flushyzmcode",
				dataType:"text",
				success:(data)=>{
					$yzmpic.attr("val",data)
					$yzm.val("").focus()
					$yzmpic.css("background-image", "url("+"flushyzmpic?ran="+Math.random()+")")
				}
			})
		},300)
		// 更换图片
	})
	
	$xialajiazaineirong  = $("#xialajiazaineirong");
	$guding = $("#guding");
	$xialajiazai = $("#xialajiazai");
	let guding = document.getElementById("guding");
	$zhezhao = $(".zhezhao");
	$guding.scroll(()=>{
		if(boo){return;}
		let s_top = guding.scrollTop;
		let xlnr_height =Number.parseInt($xialajiazaineirong.css("height"));
		let xl_height= Number.parseInt($guding.css("height"));
		if(s_top>=xlnr_height-xl_height){
			boo=true;
			flush_data();
		}
	})
	flush_data = ()=>{
		let pageindex = $xialajiazai.attr("page")
		let pagemax = $xialajiazai.attr("max")
		$.ajax({
			url:'lybflushdata',
			data:{
				fenyepageindex:parseInt(pageindex)+1
			},
			dataType:'json',
			beforeSend:()=>{
				$zhezhao.fadeToggle(speed);
			},
			success:(data)=>{
				$xialajiazai.attr("max",data.fenyepagemax)
				$xialajiazai.attr("page",data.fenyepageindex)
				data.list.forEach((ele,index)=>{
					let $hang = $(`<div class='hang' val='${ele.id}'></div>`);
					let $top = $(`<div class='top' msgid='${ele.id}'>楼层&nbsp;&nbsp;&nbsp;${ele.id}<br/>留言时间: ${formatdate(new Date(ele.datetime))}</div>`);
					let $bottom = $(`<div class='bottom'></div>`);
					let $left = $(`<div class='left' name='${ele.name}' target='${ele.target}' type='${ele.type}' msgid='${ele.id}'>${ele.type==0?"普通留言":""}${ele.type==1?"新建话题<br/><br/>"+ele.name:""}${ele.type==2?"引用词条<br/><br/>楼层"+ele.target:""}${ele.type==3?"参与话题<br/><br/>"+ele.name:""}</div>`);
					let $right = $(`<div class='right'>${ele.value}</div>`);
					$bottom.append($left);
					$bottom.append($right);
					$hang.append($top);
					$hang.append($bottom);
					$left.click(()=>{
						$new.click()
						let type = $left.attr("type")
						if(type==0){
							$valuename.val("引用词条：楼层"+$left.attr("msgid"))
							msgtype = 2
							msgtarget = $left.attr("msgid")
						}else if(type==1){
							$valuename.val("参与话题："+$left.attr("name"))
							msgtype = 3
							msgtarget = $left.attr("msgid")
							msgname = $left.attr("name")
							
						}else if(type==2){
							$valuename.val("引用词条：楼层"+$left.attr("msgid"))
							msgtype = 2
							msgtarget = $left.attr("target")
						}else if(type==3){
							$valuename.val("参与话题："+$left.attr("name"))
							msgtype = 3
							msgtarget = $left.attr("target")
							msgname = $left.attr("name")
						}
						
						$valuename.attr("disabled","true")
					})
					$top.click(()=>{
						$new.click()
						$valuename.val("引用词条：楼层"+$top.attr("msgid"))
						$valuename.attr("disabled","true")
						msgtype = 2
						msgtarget = $top.attr("msgid")
					})
					$right.text(ele.right);
					$xialajiazaineirong.append($hang);
				})
			},
			complete:()=>{
				$zhezhao.fadeToggle(speed);
				setTimeout(()=>{
					boo = false
					$flush.removeAttr("disabled");
				},speed)
			}
			
		})
	}
	$flush.click(()=>{
		$flush.attr("disabled","disabled");
		$xialajiazaineirong.empty()
		$xialajiazai.attr("page","0")
		$xialajiazai.removeAttr("max")
		$textarea.val("")
		msgname = ""
		$new.click()
		flush_data();
		$yzmpic.click();
	})
	
	$.ajax({
		url:"getTokenTeg",
		complete:()=>{
			flush_data();
			$yzmpic.click()
		}
	})
})

$(()=>{
})